<!-- 公共文件 start -->
@extends('admin.public.base')
<!-- 公共文件 end -->
<style>
    table th {
        min-width: 10px !important;
    }
    .layui-card-header {
        padding-top: 10px !important;
    }
</style>
@section('content')
    <div class="x-nav">
    <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">商品管理</a>
        <a><cite>商品分类</cite></a>
    </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
           onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
        </a>
    </div>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">
                        <button class="layui-btn" onclick="xadmin.open('添加商品分类','/admin/goods/categoryAdd',700,500)"><i class="layui-icon"></i>添加</button>
                    </div>
                    <div class="layui-card-body ">
                        <table class="layui-table layui-form">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>分类名</th>
                                <th>分类图片</th>
                                <th>备注</th>
                                <th>更新时间</th>
                                <th>操作</th>
                            </thead>
                            <tbody class="x-cate">
                            @foreach($tree as $item)
                                <tr cate-id='{{$item['id']}}' fid='{{$item['pid']}}' >
                                    <td>
                                        {{$item['id']}}
                                    </td>
                                    <td>
                                        <i class="layui-icon x-show" status='true'>&#xe623;</i>
                                        {{$item['cate_name']}}
                                    </td>
                                    <td>
                                        <img src="{{$item['cate_img']}}" style="width: 100px;height: 100px">
                                    </td>
                                    <td>
                                        {{$item['remark']}}
                                    </td>
                                    <td>
                                        {{$item['updated_at']}}
                                    </td>
                                    <td class="td-manage">
                                        <button class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('编辑商品分类','/admin/goods/categoryEdit?id={{$item['id']}}',700,500)" ><i class="layui-icon">&#xe642;</i>编辑</button>
                                        <button class="layui-btn-danger layui-btn layui-btn-xs"  onclick="cate_del(this,{{$item['id']}})" href="javascript:;" ><i class="layui-icon">&#xe640;</i>删除</button>
                                    </td>
                                </tr>
                                @if($item['son'] && is_array($item['son']))
                                    @foreach($item['son'] as $val)
                                        <tr cate-id='{{$val['id']}}' fid='{{$val['pid']}}' >
                                            <td>
                                                {{$val['id']}}
                                            </td>
                                            <td>
                                                &nbsp;&nbsp;&nbsp;&nbsp;
                                                |-- {{$val['cate_name']}}
                                            </td>
                                            <td>
                                                <img src="{{$item['cate_img']}}" style="width: 100px;height: 100px">
                                            </td>
                                            <td>
                                                {{$val['remark']}}
                                            </td>
                                            <td>
                                                {{$val['updated_at']}}
                                            </td>
                                            <td class="td-manage">
                                                <button class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('编辑商品分类','/admin/goods/categoryEdit?id={{$val['id']}}',700,500)" ><i class="layui-icon">&#xe642;</i>编辑</button>
                                                <button class="layui-btn-danger layui-btn layui-btn-xs"  onclick="cate_del(this,{{$val['id']}})" href="javascript:;" ><i class="layui-icon">&#xe640;</i>删除</button>
                                            </td>
                                        </tr>
                                    @endforeach
                                @endif
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        layui.use(['form'], function(){
            form = layui.form;

        });

        // 删除
        function cate_del(obj,id){
            layer.confirm('确认要删除吗？',function(index){
                //发异步删除数据
                $.ajax({
                    type: 'POST',
                    url: '/admin/goods/categoryDel',
                    data: {
                        id: id
                    },
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            $(obj).parents("tr").remove();
                            layer.msg('已删除',{icon:1,time:2000});
                        } else {
                            layer.alert(data.msg, {icon: 7, time: 2000, closeBtn: false, btn: []});
                        }
                    },
                    error: function (xhr, type) {
                        alert('Ajax error!')
                    }
                });
            });
        }

        // 分类展开收起的分类的逻辑
        $(function(){
            $("tbody.x-cate tr[fid!='0']").hide();
            // 栏目多级显示效果
            $('.x-show').click(function () {
                if($(this).attr('status')=='true'){
                    $(this).html('&#xe625;');
                    $(this).attr('status','false');
                    cateId = $(this).parents('tr').attr('cate-id');
                    $("tbody tr[fid="+cateId+"]").show();
                }else{
                    cateIds = [];
                    $(this).html('&#xe623;');
                    $(this).attr('status','true');
                    cateId = $(this).parents('tr').attr('cate-id');
                    getCateId(cateId);
                    for (var i in cateIds) {
                        $("tbody tr[cate-id="+cateIds[i]+"]").hide().find('.x-show').html('&#xe623;').attr('status','true');
                    }
                }
            })
        })

        var cateIds = [];
        function getCateId(cateId) {
            $("tbody tr[fid="+cateId+"]").each(function(index, el) {
                id = $(el).attr('cate-id');
                cateIds.push(id);
                getCateId(id);
            });
        }
    </script>
@endsection
